﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ProductManagement;

public partial class Modules_Production_ucProductAccessory : System.Web.UI.UserControl
{
    Products _products = new Products();
    void SearchAccessory()
    {
        try
        {
            string toSearch = string.Empty;
            gvwAccessory.Visible = true;
            int count = (int)Session["Product_selectedProducts"];
            string productID = string.Empty;
            if (Session["Product_FromControl"].ToString() == "Production/ucAddAccessory.ascx")
                toSearch = "new"; // show newly added accessories on top
            DataSet dsAccessory;
            _products.SearchAccessory(productID, toSearch, out dsAccessory);
            gvwAccessory.DataSource = dsAccessory;           
            gvwAccessory.Columns[1].Visible = true;

            gvwAccessory.DataBind();
            //if (Session["Product_btnAddAccessory"] == "false")
            //{
            //    gvwAccessory.Columns[0].Visible = false;
            //    gvwAccessory.Columns[7].Visible = true;
            //}
            //if ((Session["Product_btnAddAccessory"] == "true") || (count > 1))
            //{
            //    gvwAccessory.Columns[0].Visible = true;
            //    gvwAccessory.Columns[7].Visible = false;
            //}
            gvwAccessory.Columns[1].Visible = false;

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Search Accessories Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

    void GetIDs()
    {
        try
        {
            int countAccessory = 0;            
            string[] accessoryID = new string[gvwAccessory.Rows.Count];

            for (int i = 0; i < gvwAccessory.Rows.Count; i++)  //checking selected Accessories
            {
                accessoryID[i] = string.Empty;
                CheckBox chkGrid = (CheckBox)(gvwAccessory.Rows[i].Cells[0].FindControl("chkEditDelete"));
                if (chkGrid.Checked == true)
                {
                    accessoryID[countAccessory] = gvwAccessory.Rows[i].Cells[1].Text;
                    countAccessory++;
                }
            }

            Session["Product_AddAccessoryIDs"] = accessoryID;
            Session["Product_selectedAccessories"] = countAccessory;
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Get IDs Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void AddAccessory()
    {
        try
        {
            int countAccessory = (int)Session["Product_selectedAccessories"];// no: of accessories selected(checked in the grid)           
            if (Session["Product_Add/EditProduct"] == "Added")  //if accessory is to be added to newly entered product
                Session["Product_selectedProducts"] = 1;
            int countProducts = (int)Session["Product_selectedProducts"];//no: of products selected

            string[] productIDs = (string[])Session["Product_IDs"];
            string[] accessoryIDs = (string[])Session["Product_AddAccessoryIDs"];

            for (int i = 0; i < countProducts; i++)
                for (int j = 0; j < countAccessory; j++)
                    _products.AddProductAccessory(productIDs[i], accessoryIDs[j]);

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add Product - Accessory Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] ID = new string[1];
            if (Session["Product_Add/EditProduct"].ToString() == "Added") //if new product is saved
            {
                ID[0] = _products.GetMaxProductID();
                Session["Product_IDs"] = ID;
            }
            SearchAccessory();
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "Production/ucAddProduct.ascx";
        Session["Product_FromControl"] = "Production/ucProductAccessory.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void btnSaveAccessory_Click(object sender, EventArgs e)
    {       
        GetIDs();
        AddAccessory();              

        if ((int)Session["Product_selectedProducts"] == 1) //if accessory is added to only 1 product, show those accessories
        {
            SearchAccessory();            
        }
        
        Response.Write("<script>alert('Accessories Added Successfully !')</script>");
       
    }

    protected void btnAddAccessory_Click(object sender, EventArgs e)
    {
        Session["Product_Add/EditAccessory"] = "Add";
        Session["Product_FromControl"] = "Production/ucProductAccessory.ascx";
        Session["UserManager_Control"] = "Production/ucAddAccessory.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void gvwAccessory_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwAccessory.PageIndex = e.NewPageIndex;
        SearchAccessory();
    }
    //protected void gvwAccessory_RowCommand(object sender, GridViewCommandEventArgs e)
    //{
    //    int index = Convert.ToInt32(e.CommandArgument);
    //    string productID = ((string[])Session["Product_IDs"])[0].ToString();
    //    string accessoryID = gvwAccessory.Rows[index].Cells[1].Text;
    //    _products.DeleteProductAccessory(productID, accessoryID);
    //    SearchAccessory();
    //}
    
}
